﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

namespace KATS_Client.Client
{
    /// <summary>
    /// The user data for the server
    /// </summary>
    public static class User
    {
        /// <summary>
        /// Static constructor
        /// </summary>
        static User()
        {
            Load();
        }

        /// <summary>Update the login details</summary>
        /// <param name="username">The username</param>
        /// <param name="password">The password</param>
        public static void Update(string username, string password)
        {
            Username = username;
            Password = password;
            Save();
        }

        /// <summary>
        /// Is there a user data file?
        /// </summary>
        /// <returns></returns>
        public static bool Exists()
        {
            return System.IO.File.Exists(Properties.Settings.Default.UserData);
        }

        /// <summary>
        /// Load the user data from the file
        /// </summary>
        static void Load()
        {
            if (!Exists())
            {
                // there is no default user data file currently.
                // make a default one
                Update("Admin", "Password");
            }
            XDocument document = XDocument.Load(Properties.Settings.Default.UserData);
            Username = document.Root.Element("Username").Value;
            Password = document.Root.Element("Password").Value;

        }


        /// <summary>
        /// Save the user data to the file
        /// </summary>
        static void Save()
        {
            int rv = ProgressReporting.Start("Saving user data");
            try
            {
                XDocument document =
                     new XDocument(
                         new XDeclaration("1.0", "uft-8", "yes"),
                         new XComment("User Data"),
                         new XElement("User",
                             new XElement("Username", Username),
                             new XElement("Password", Password)
                             ));
                document.Save(Properties.Settings.Default.UserData);
                ProgressReporting.End(rv, "Saving user data");
            }
            catch (Exception e)
            {
                // catch any errors and display them on the console
                ProgressReporting.Failed("Could not save user data to file\n-> " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }

        /// <summary>The username</summary>
        static string _Username = string.Empty;

        /// <summary>The username</summary>
        public static string Username
        {
            get { return _Username; }
            private set { _Username = value; }
        }

        /// <summary>The password</summary>
        static string _Password = string.Empty;

        /// <summary>The password</summary>
        public static string Password
        {
            get { return _Password; }
            private set { _Password = value; }
        }
    }
}
